package md8800;

import gnu.io.PortInUseException;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class StartUp extends javax.swing.JFrame
{

	private static final long serialVersionUID = 4399837270357131997L;
	private JButton jButton1;
	private JButton jButton_disco;
	private JButton jButton_time;
	private JButton jButton_send;
	private JLabel jLabel1;
	private JEditorPane jEditorPane1;
	private controller _controller;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				StartUp inst = new StartUp();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public StartUp()
	{
		super();
		_controller = new controller();
		initGUI();
	}

	private void initGUI()
	{
		try
		{
			getContentPane().setLayout(null);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Connect");
				jButton1.setBounds(55, 77, 92, 23);
				jButton1.addMouseListener(new MouseAdapter()
				{
					public void mouseReleased(MouseEvent evt)
					{
						jButton1MouseReleased(evt);
					}

					public void mouseClicked(MouseEvent evt)
					{
						jButton1MouseClicked(evt);
					}
				});
			}
			{
				jEditorPane1 = new JEditorPane();
				getContentPane().add(jEditorPane1);
				jEditorPane1.setText("jEditorPane1");
				jEditorPane1.setBounds(40, 12, 173, 20);
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setBounds(197, 95, 58, 16);
				jLabel1.setText("not connected");
			}
			{
				jButton_send = new JButton();
				getContentPane().add(jButton_send);
				jButton_send.setText("Send");
				jButton_send.setBounds(219, 12, 98, 23);
				jButton_send.addMouseListener(new MouseAdapter()
				{
					public void mousePressed(MouseEvent evt)
					{
						jButton_sendMousePressed(evt);
					}

					public void mouseClicked(MouseEvent evt)
					{
						jButton_sendMouseClicked(evt);
					}
				});
			}
			{
				jButton_time = new JButton();
				getContentPane().add(jButton_time);
				jButton_time.setText("Time Set");
				jButton_time.setBounds(91, 32, 106, 23);
				jButton_time.addMouseListener(new MouseAdapter()
				{
					public void mousePressed(MouseEvent evt)
					{
						jButton_timeMousePressed(evt);
					}
				});
			}
			{
				jButton_disco = new JButton();
				getContentPane().add(jButton_disco);
				jButton_disco.setText("Disconnect");
				jButton_disco.setBounds(55, 111, 131, 23);
				jButton_disco.addMouseListener(new MouseAdapter()
				{
					public void mousePressed(MouseEvent evt)
					{
						jButton_discoMousePressed(evt);
					}
				});
			}
			pack();
			setSize(400, 300);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	protected void jButton_sendMouseClicked(MouseEvent evt)
	{
		// String s = jEditorPane1.getText();
		//
		// _controller.sendString(s);
		// Time t = new Time();
		// _controller.sendString(op_codes.SET_TIME + t.gibZeit());

	}

	private void jButton1MouseClicked(MouseEvent evt)
	{
		//
		// if (_controller.connect())
		// {
		// jLabel1.setText("Connected");
		// }
		// else
		// {
		// jLabel1.setText("Connection error");
		// }
	}

	private void jButton_sendMousePressed(MouseEvent evt)
	{
		String s = jEditorPane1.getText();
		_controller.sendString(s);

	}

	private void jButton_timeMousePressed(MouseEvent evt)
	{
		Time t = new Time();
		_controller.sendString(op_codes.EURO_TIME.getCode());
		_controller.sendString(op_codes.SET_TIME.getCode() + t.gibZeit());
	}

	private void jButton1MouseReleased(MouseEvent evt)
	{

		try
		{
			if (_controller.connect())
			{
				jLabel1.setText("Connected");
			}
			else
			{
				jLabel1.setText("Connection error");
			}
		}
		catch (PortInUseException e)
		{
			jLabel1.setText("Port in Use");
			_controller.networkDisconnected(1);
		}

	}

	private void jButton_discoMousePressed(MouseEvent evt)
	{
		_controller.networkDisconnected(1);
		jLabel1.setText("Disconnected");
	}
}
